<?php

namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\Quality;
use App\Models\QualityInspect;
use App\Models\SafeInspect;
use App\Models\Schedule;
use App\Models\ScheduleRecord;
use App\Models\TempInspect;
use App\Models\User;
use App\Services\UserService;
use Illuminate\Http\Request;

class StatisticsController extends Controller
{
    public function index(Request $Request) {
        $date = Date('Y-m-d H:i:s',time());
        //进度
        $Schedule = [];
        $Schedule['count'] = Schedule::where('project_id',$Request->project_id)->count();
        $Schedule['record'] = ScheduleRecord::where('type',3)->where('project_id',$Request->project_id)->count();
        $Schedule['percentage'] = $Schedule['record'] == 0 ? 0 : number_format(($Schedule['record'] / $Schedule['count']) * 100,2);

        $Schedule_date = [];
        $currentTime = time();
        for ($i = 6; $i >= 0; $i--) {
            $timestamp = strtotime("-$i days", $currentTime);
            $Schedule_date[] = date('Y-m-d', $timestamp);
        }

        $Schedule_total = Schedule::select('start_date','id')->whereIn('start_date',$Schedule_date)->where('project_id',$Request->project_id)->get();
        $Schedule_total = $Schedule_total->groupBy('start_date');

        $Schedule_complete = ScheduleRecord::select('front_date','id')->whereIn('front_date',$Schedule_date)->where('type',3)->where('project_id',$Request->project_id)->get();
        $Schedule_complete = $Schedule_complete->groupBy('front_date');
        $Schedule['date'] = [];
        foreach ($Schedule_date as $key => $value) {
            $arr = [];
            $arr['date'] = $value;
            $arr['date_complete'] = 0;
            $arr['date_total'] = 0;
            foreach ($Schedule_complete as $k => $v) {
                if($k == $value){
                    $arr['date_complete'] = $v->count();
                }
            }
            foreach ($Schedule_total as $k => $v) {
                if($k == $value){
                    $arr['date_total'] = $v->count();
                }
            }
            $Schedule['date'][] = $arr;
        }


        $Today = [];
        $Today['schedule'] = Schedule::whereDate('start_date','<=',$date)->whereDate('end_date','>=',$date)->count();
        $Today['schedule_complete'] = ScheduleRecord::where('type',3)->where('project_id',$Request->project_id)->count();

        $Today['safe'] = 0;
        $Today['safe_complete'] = SafeInspect::where('project_id',$Request->project_id)->count();

        $Today['quality'] = Quality::whereDate('start_date','<=',$date)->whereDate('end_date','>=',$date)->count();
        $Today['quality_complete'] = QualityInspect::where('project_id',$Request->project_id)->count();

        $Today['temporary'] = TempInspect::where('project_id',$Request->project_id)->count();
        $Today['temporary_complete'] = QualityInspect::where('project_id',$Request->project_id)->count();

        $User = User::select('id','name','role_id')->where('role_id','>',0)->with('role')->withCount('safes')->withCount('schedules')->withCount('qualitys')->withCount('temps')->get();

        $Project = Project::find($Request->project_id);
        $Project->remainDuration = $Project->remainDuration($Project->end_date);
        $Project->duration = $Project->duration($Project->start_date,$Project->end_date);

        $UserService = new UserService();
        $Three = $UserService->statisticsThree($Request->project_id);
        return response()->json(['code' => '200', 'Project' => $Project,'Schedule' => $Schedule, 'Today' => $Today,  'Three' => $Three,'User' => $User]);
    }

    public function tempComplete(Request $Request) {
        $date = Date('Y-m-d H:i:s',time());
        $sql = TempInspect::orderBy('created_at','desc');
        if($Request->type == 'today'){
            $sql->whereDate('created_at','<=',$date)->whereDate('created_at','>=',$date);
        }
        $data = $sql->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function temp(Request $Request) {
        $data = TempInspect::with('person')->orderBy('created_at','desc');
        if($Request->id){
            $data = $data->where('person_id',$Request->id);
        }
        $data = $data->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    
}
